package com.example.group_20.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.group_20.entity.Admin;
import com.example.group_20.entity.Group;
import com.example.group_20.entity.Students;
import com.example.group_20.entity.Teachers;
import com.example.group_20.mapper.*;
import com.example.group_20.service.AdminService;
import com.example.group_20.utils.JwtUtils;
import com.example.group_20.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author: 栋
 * @data: 2023/8/29
 * @descriptions:
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private StudentsMapper studentsMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private FilesMapper filesMapper;
    @Autowired
    private SLogMapper sLogMapper;
    @Autowired
    private TLogMapper tLogMapper;
    @Override
    public Result login(String id, String pwd) {

        Admin admin = adminMapper.findByid(id);

        if(admin!=null){
            if(pwd.equals(admin.getPassword())) {
                String token = JwtUtils.generateToken(id);
                return Result.Ok().data("token", token);
            }
        }
        return Result.Error().msg("Account and password are incorrect.");
    }

    @Override
    public Result logout() {
        return Result.Ok().data("sucess","sucess");
    }

    @Override
    public Result getInfor(String token) {
        String id = JwtUtils.getClaimsByToken(token).getSubject();
        Admin admin=adminMapper.findByid(id);
        return Result.Ok().data("admin",admin);
    }

    @Override
    public Result showStuInfor() {
        List<Students> students = studentsMapper.findAll();
        List<Group> groups = groupMapper.findAll();
        return Result.Ok()
                .data("students",students)
                .data("groups",groups);
    }

    @Override
    public Result showTeaInfor() {
        List<Teachers> teachers = teacherMapper.findAll();
        return Result.Ok().data("teachers",teachers);
    }

    @Override
    public Result showGroupInfor() {
        List<Group> groups = groupMapper.findAll();
        List<Teachers> teachers = teacherMapper.findAll();
        return Result.Ok()
                .data("groups",groups)
                .data("teachers",teachers);
    }

    @Override
    public Result deleteTeacher(Map<String, Object> mp) {
        StringBuilder t_id=new StringBuilder("");
        if(mp.containsKey("t_id")){
            t_id.append(mp.get("t_id"));
        }
        List<Group> groups = groupMapper.findByTid(t_id.toString());
        if(!groups.isEmpty()){
            return Result.Error().msg("删除失败,该老师已分配有小组").data("message","error");
        } else {
            int res = teacherMapper.deleteById(t_id.toString());
            return Result.Ok().msg("删除成功").data("message", "success");
        }
    }

    @Override
    public Result addTeacher(Map<String, Object> mp) {
        StringBuilder t_id=new StringBuilder("");
        StringBuilder t_name=new StringBuilder("");
        StringBuilder password=new StringBuilder("");
        if(mp.containsKey("t_id")){
            t_id.append(mp.get("t_id"));
        }
        if(mp.containsKey("t_name")){
            t_name.append(mp.get("t_name"));
        }
        if(mp.containsKey("password")){
            password.append(mp.get("password"));
        }
        Teachers teachers=new Teachers(t_id.toString(),t_name.toString(),null,null,password.toString());
        Teachers teachers1 = teacherMapper.findByid(t_id.toString());
        if(teachers1!=null){
            return Result.Error().data("message","error").msg("该老师已存在，请检查工号");
        }else {
            teacherMapper.insert(teachers);
            return Result.Ok().data("message","success").msg("添加成功");
        }

    }

    @Override
    public Result addGroup(Map<String, Object> mp) {
        StringBuilder group_id=new StringBuilder("");
        if(mp.containsKey("group_id")){
            group_id.append(mp.get("group_id"));
        }
        Group group=new Group(Integer.valueOf(group_id.toString()),null,null,null,null,null,null,null,null,0,null,null);

        groupMapper.addGroup(group);

        return Result.Ok().msg("添加成功").data("message","success");
    }

    @Override
    public Result selectTeacher(Map<String, Object> mp) {
        StringBuilder group_id=new StringBuilder("");
        StringBuilder t_id=new StringBuilder("");
        if(mp.containsKey("group_id")){
            group_id.append(mp.get("group_id"));
        }
        if(mp.containsKey("t_id")){
            t_id.append(mp.get("t_id"));
        }
        Group group = groupMapper.findByid(Integer.valueOf(group_id.toString()));
        group.setT_id(t_id.toString());
        UpdateWrapper<Group> groupUpdateWrapper=new UpdateWrapper<>();
        groupUpdateWrapper.eq("group_id",group.getGroup_id());
        int update = groupMapper.update(group, groupUpdateWrapper);
        if(update!=0){
            return Result.Ok().data("message","success")
                    .msg("分配成功");
        }else{
            return Result.Ok().data("message","error")
                    .msg("分配失败");
        }
    }

    @Override
    public Result deleteGroup(Map<String, Object> mp) {
        StringBuilder group_id=new StringBuilder("");
        if(mp.containsKey("group_id")){
            group_id.append(mp.get("group_id"));
        }
        UpdateWrapper<Group> groupWrapper=new UpdateWrapper<>();
        groupWrapper.eq("group_id",Integer.valueOf(group_id.toString()));
        groupMapper.delete(groupWrapper);
        groupMapper.updateGroupId(Integer.valueOf(group_id.toString()));
        studentsMapper.updateStudentGroupId(Integer.valueOf(group_id.toString()));
        sLogMapper.updateSLogGroupId(Integer.valueOf(group_id.toString()));
        tLogMapper.updateTLogGroupId(Integer.valueOf(group_id.toString()));
        filesMapper.updateFilesGroupId(Integer.valueOf(group_id.toString()));
        return Result.Ok().msg("删除成功").data("message","success");
    }
}
